package nthu.web.listener;


import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;


public class LoginSessionListener implements HttpSessionListener {
    private static int count;
    
    /*
	String databaseIP = "localhost";
	String userName = "essweb";
	String userPassword = "essess";
	String targetDataBase ="pipezon";
	String jdbcURL = "jdbc:mysql://" + databaseIP  + "/" + targetDataBase + "?useUnicode=true&amp;characterEncoding=UTF-8";
    */
   
    public static int getCount() {
        return count;
    }
    /*
    public static Map<String,String> getMap() {
        return sessionsMap;
    }
    
    public static void putMap(String key,String username) {
    	sessionsMap.put(key,username); 
    }
    
    public static void removefromMap(String key) {
        sessionsMap.remove(key);
    }
    */
    public void sessionCreated(HttpSessionEvent se) { //session創造時引發的事件
        LoginSessionListener.count++;

        /*
        HttpSession session = se.getSession();
        String name = (String)session.getAttribute("username");
        try {
        	
        	Class.forName("com.mysql.jdbc.Driver");
        	Connection conn = DriverManager.getConnection(jdbcURL, userName, userPassword);
            Statement stmt = conn.createStatement();
            String sql = "insert into onlineuser(name)values('"+name+"')";
            stmt.executeUpdate(sql);
            
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException e) {
			e.printStackTrace();
		}*/
    	
    }


    public void sessionDestroyed(HttpSessionEvent se) {
        LoginSessionListener.count--;
        
        
    }
}